package vaani.internal.purple;

import im.pidgin.purple.PurpleInterface;
import java.util.Vector;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;


/**
 *
 * @author sourcemorph
 */

public class PidginHandler {

    private static String PurpleObjectPath = "/im/pidgin/purple/PurpleObject";
    private static String PurpleServiceBusName = "im.pidgin.purple.PurpleService";
    private DBusConnection conn;
  

    public boolean makeConnection() {
        try {
            conn = DBusConnection.getConnection(DBusConnection.SESSION);
        } catch(DBusException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public void closeConnection() {
        conn.disconnect();
    }

    public PurpleInterface getPurpleObject() {

        PurpleInterface purpleObject;
        try {
            purpleObject = (PurpleInterface) conn.getRemoteObject(PurpleServiceBusName, PurpleObjectPath);
        } catch(DBusException ex) {
            System.out.println("Could not get purple object");
            ex.printStackTrace();
            return null;
        }
        return purpleObject;
    }

    public Vector<Integer> getConnectedAccounts() {
        Vector<Integer> connected = new Vector<Integer>();
        PurpleInterface purpleObject = getPurpleObject();
        for(int account : purpleObject.PurpleAccountsGetAllActive()) {
            if(purpleObject.PurpleAccountIsConnected(account) != 0)
                connected.add(account);
        }
        return connected;
    }

    public int getBuddy(Buddy buddy) {
        PurpleInterface purpleObject = getPurpleObject();
        int temp;
        for(int account : getConnectedAccounts()) {
            if((temp = purpleObject.PurpleFindBuddy(account, buddy.getName())) != 0) {
                return temp;
            }
        }
        return 0;
    }

    public int getAccount(InstantMessageAccount account) {
        return getAccount(account.getUserID(), account.getProtocol());
    }

    public int getAccount(String username, String protocol) {
        PurpleInterface purpleObject = getPurpleObject();
        /*have to figure out how to use the protocol, because
         * in blist.xml protocol names are different from
         * ones returned by the function in the purple
         * interface*/
        for(int a : getConnectedAccounts()) {
            if(purpleObject.PurpleAccountGetUsername(a).equalsIgnoreCase(username)) {
                return a;
            }
        }
        return 0;
    }

    //method #1 for InstantMessageManager
    public boolean startNewConversation(InstantMessageAccount account, Buddy buddy) {
        PurpleInterface purpleObject = getPurpleObject();
        purpleObject.PurpleConversationNew((int) 1, getAccount(account), buddy.getName());
        return false;
    }

    /*trial
    public static void main(String [] args) {
        PidginHandler p = new PidginHandler();
        p.makeConnection();
        PurpleInterface i = p.getPurpleObject();
        String uname = i.PurpleAccountGetUsername(p.getConnectedAccounts().firstElement());
        String proto = i.PurpleAccountGetProtocolName(p.getConnectedAccounts().firstElement());
        System.out.println(p.getAccount(uname, proto));
        p.closeConnection();
    }*/
}
